'''
Created on 2010-1-3

@author: wangyongtao
'''
import os
import logging
from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

DEBUG = True
DEFAULT_LIMIT = 24 

class BaseRequestHandler(webapp.RequestHandler):
    def generate(self, template_name, template_values = {}):
        logging.debug('base output method')
        values = {
            'request' : self.request,
            'user' : users.get_current_user(),
            'is_admin': users.is_current_user_admin(),
            'login_url': users.create_login_url(self.request.uri),
            'logout_url': users.create_logout_url('http://%s/' % (self.request.host,)),
            'debug' : self.request.get('debug', DEBUG),
            'app_name' : 'Taobility Software',
        }
        logging.debug('user-agent:%s'%self.request.headers['User-Agent'])
        values.update(template_values)
        dir = os.path.dirname(__file__)
        path = os.path.join(dir, os.path.join('templates', template_name)) 
        self.response.out.write(template.render(path, values))
        
    def pageHandler(self):
        page = self.request.get('page', None)
        if not page or int(page) < 1:
            page = 1
        else:
            page = int(page)
        return page
    
    def limitHandler(self):
        limit = self.request.get('limit', None)
        if not limit or int(limit) < 1:
            limit = DEFAULT_LIMIT
        else:
            limit = int(limit)
        logging.debug('limit: %d'%limit)
        return limit
        